<?php
/*
 * Created on 2012-12-21
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

/*
 * 74.86.25.136    10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
75.126.2.212    10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
75.126.255.162  10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
75.126.255.164  10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
85.25.176.109   10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
98.158.157.21   10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
98.158.157.22   10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
98.158.157.44   10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
98.158.158.98   10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
118.142.51.162  10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
173.45.75.66    10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
173.45.76.242   10.89.0.1       255.255.255.255 UGH   101    0        0 tun2
173.45.93.162   10.89.0.1       255.255.255.255 UGH   101    0  
 */

//1.验证用户名和密码
require_once (dirname(__FILE__) . "/../config/config.php");
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$mac = $_REQUEST['mac'];
if (!isset ($_REQUEST['mac'])) {
	if ($username == "" || $password == "") {
		$error1 = array (
			"error1" => "The username or password can not be blank!"
		);
		echo json_encode($error1);
		die();
	}

}
//获取用户判断mac是否存在，存在的话用另外一种查法！
if (isset ($_REQUEST['mac'])) {
	$row = checkMac($mac);
	if ($row['equipment_id'] == "") {
		$error_message = "<error>设备不存在!</error>\n";
		outPutXml($error_message);
	}
	if ($row['updater'] == "") {
		$error_message = "<error>没有为此设备分配用户!</error>\n";
		outPutXml($error_message);
	}
	if ($row['service_id'] == "") {
		$error_message = "<error>服务ID不存在!</error>\n";
		outPutXml($error_message);
	}
	$user_id = $row['updater'];
} else {
	$user_id = checkLogin($username, $password);
}
if ($user_id == "") {
	$error2 = array (
		"error2" => "The username or password is wrong!"
	);
	echo json_encode($error2);
	die();
}
//2.输出内容
/* $result[0]=array("netwok"=>"208.78.70.34","maskbit"=>"32","tag"=>"1");
 $result[1]=array("netwok"=>"204.13.250.34","maskbit"=>"32","tag"=>"1");
 $result[2]=array("netwok"=>"211.101.171.217","maskbit"=>"32","tag"=>"2");
 $result[3]=array("netwok"=>"123.125.34.30","maskbit"=>"32","tag"=>"2");*/
$row = checkVPN($user_id);
if ($row == "") {
	$error3 = array (
		"error3" => "The user don't order VPN Service!"
	);
	echo json_encode($error3);
	die();
}
$result = getRouting($user_id);
echo json_encode($result);
/**
 * 验证登陆
 * @param unknown $username
 * @param unknown $password
 * @return Ambigous <>
 */
function checkLogin($username, $password) {
	$sql = "select * from zy_user where user_name = '" . $username . "' and password = '" . $password . "'";
	$result = mysql_query($sql);
	$rs = mysql_fetch_array($result);
	return $rs['user_id'];
}
/**
 * 检查VPN
 * @param unknown $user_id
 * @return multitype:unknown Ambigous <>
 */
function checkVPN($user_id) {
	$sql = "SELECT * FROM zy_service AS a LEFT JOIN zy_user_service AS b ON a.`service_id` = b.`service_id` WHERE (a.code='vpnsoftware' or a.code='smartrouting') and b.`user_id` = " . $user_id;
	$result = mysql_query($sql);
	$i = 0;
	while ($rs = mysql_fetch_array($result)) {
		$rs2[$i] = array (
			"service_id" => $rs['service_id'],
			"code" => $rs['code']
		);
		$i++;
	}
	return $rs2;
}
/**
 *1. 获取返回API，返回结果格式为json_code
 *2.判断用户是否开启全路由，开启全路由则返回全路由API
 * @param unknown $user_id
 * @return multitype:unknown Ambigous <> multitype:unknown Ambigous <>
 */
function getRouting($user_id) {
	//2.判断用户是否开启全路由，开启全路由则返回全路由API
	$sql = "select * from zy_full_routing where user_id=" . $user_id;
	$rs6 = mysql_query($sql) or die(mysql_error());
	$row = mysql_fetch_array($rs6);
	$getSystemTime = getdate(); //获取当前系统时间
	$days = $getSystemTime['0'] - strtotime($row['open_time']); //当前系统时间减去路由开启时间
	$spendsTime = abs($days / 3600); //除上3600秒，得出全路由开启时长,单位小时'
	
	if ($row[mark]==1 && $spendsTime <= 2) {
		$rs2[] = array (
			"vpnid" => $row['vpnlist_id'],
			"network" => "0.0.0.0",
			"maskbit" => "1",
			"tag" => "1"
		);
		$rs2[] = array (
			"vpnid" => $row['vpnlist_id'],
			"network" => "128.0.0.0",
			"maskbit" => "1",
			"tag" =>"1"
		);
	} else {
		//当智能路由关闭的时候
			if($row['smart_mark']==0){
				$rs2[] = array (
				"vpnid" => "0",
				"network" => "0.0.0.0",
				"maskbit" => "0",
				"tag" => "0"
			);
		}else {
		//当智能路由开启的时候
		$sql = "select * from zy_routing where vpnid IN(SELECT id FROM zy_vpnlist WHERE id IN (SELECT b.field1 FROM zy_user_service a LEFT JOIN zy_login_list b ON a.`user_service_id`=b.user_service_id WHERE a.`user_id`=" . $user_id . " AND a.`package_id` IN (SELECT package_id FROM zy_package WHERE service_id IN
									(select service_id from zy_service where code = 'smartrouting')
									))) AND  Ispublic = 1 and IP NOT IN(select IP from zy_routing where user_id ='" . $user_id . "')";
		
		$result = mysql_query($sql);
		$i = 0;
		while ($rs = mysql_fetch_array($result)) {
			$rs2[$i] = array (
				"vpnid" => $rs['vpnid'],
				"network" => $rs['IP'],
				"maskbit" => $rs['maskbit'],
				"tag" => $rs['tag']
			);
			$i++;
		}
		$sql3 = "select * from zy_routing where vpnid IN (SELECT id FROM zy_vpnlist WHERE id IN (SELECT b.field1 FROM zy_user_service a LEFT JOIN zy_login_list b ON a.`user_service_id`=b.user_service_id WHERE a.`user_id`=" . $user_id . " AND a.`package_id` IN (SELECT package_id FROM zy_package WHERE service_id IN
									(select service_id from zy_service where code = 'smartrouting')
									))) AND (tag = 1 or tag =2) And user_id=" . $user_id;
		
		$result3 = mysql_query($sql3);
		while ($rs3 = mysql_fetch_array($result3)) {
			$rs2[$i] = array (
				"vpnid" => $rs3['vpnid'],
				"network" => $rs3['IP'],
				"maskbit" => $rs3['maskbit'],
				"tag" => $rs3['tag']
			);
			$i++;
		}
	}
	}
	return $rs2;
}
function checkMac($mac) {
	$sql = "select * from zy_equipment where MacAddress='" . $mac . "'";
	$rs = mysql_query($sql);
	if ($row = mysql_fetch_array($rs)) {
		return $row;
	}
	return null;
}
?>